package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.dtos.ChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.service.WmChannelService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;


import java.util.List;

/**
 * @Author: JW
 * @Description: TODO
 * @DateTime: 2022-02-11 20:21
 **/
@Service
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {

    @Override
    public ResponseResult getChannel() {

        List<WmChannel> list = list();

        return ResponseResult.okResult(list);
    }

    /**
     * @param channelDto
     * @Author: JW
     * @Description: TODO 查询获取频道 分页
     * @Date: 20:36 2022-02-18
     * @Param: [channelDto]
     * @return: com.heima.model.common.dtos.PageResponseResult
     */
    @Override
    public PageResponseResult find(ChannelDto channelDto) {

        //参数校验
        if (ObjectUtils.isEmpty(channelDto)){

            throw new RuntimeException("参数无效");
        }

        //分页条件
        Page<WmChannel> page = new Page<>(channelDto.getPage(),channelDto.getSize());

        //查询条件
        LambdaQueryWrapper<WmChannel> queryWrapper = Wrappers.lambdaQuery(WmChannel.class).like(
                !StringUtils.isEmpty(channelDto.getName()),
                WmChannel::getName, channelDto.getName());

        //分页查询
        page(page,queryWrapper);

        PageResponseResult result = new PageResponseResult(channelDto.getPage(),
                channelDto.getSize(), (int) page.getTotal());

        result.setData(page.getRecords());

        return result;
    }
}
